package com.study.lihan.letcode;

import java.util.HashMap;
import java.util.HashSet;

/**
 * @Author: lihan
 * @Date : 2021/12/18- 12
 * @DESCRIPTION: com.study.lihan.letcode
 * @version: 1.0
 */
public class My3 {
    public static void main(String[] args) {

        String s= "abcabcbb";
        int result = lengthOfLongestSubstring(s);
        System.out.println(result);
    }
    public static int lengthOfLongestSubstring(String s) {
        if(s == null || s.equals(""))return 0;
        int len = s.length();
        int max = 1;
        char[] chars = s.toCharArray();
        HashMap<Character, Integer> map = new HashMap<>();
        for (int start = 0,end =0; end <len ; end++) {
            char cha = chars[end];
            if(map.containsKey(cha)){
               int beforeIndex = map.get(cha);
               start = Math.max(beforeIndex+1,start);
            }
            map.put(cha,end);
            max = Math.max(end -start+1,max);

        }
        return  max;
    }


}
